home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / others / ole_101.zip / PATRON.ZIP / OLELINK2.C < prev    next >
C/C++ Source or Header  |  1992-04-13  |  10KB  |  349 lines

  1. /*
  2.  * OLELINK2.C
  3.  *
  4.  * Helper functions for the Links dialog:
  5.  *  EnableLinkButtons:    Enables or disables links dialog buttons
  6.  *                        depending on the selections.
  7.  *
  8.  *  CchLinkStringCreate:  Parses ObjectLink data and creates a string
  9.  *                        of the object name, link file, selection, and
  10.  *                        update status, separated by tabs.
  11.  *
  12.  *  LinkStringChange:     Replaces one string in a listbox with another,
  13.  *                        preserving item data and selection state.
  14.  *
  15.  *
  16.  * Copyright(c) Microsoft Corp. 1992 All Rights Reserved
  17.  */
  18.  
  19.  
  20. #include <windows.h>
  21. #include <ole.h>
  22. #include "register.h"
  23. #include "oclient.h"
  24.  
  25.  
  26. extern WORD  cxList;
  27. extern HWND  hgList;
  28.  
  29.  
  30.  
  31. /*
  32.  * EnableLinkButtons
  33.  *
  34.  * Purpose:
  35.  *  Enables or disables various buttons in the Links dialog as appropriate
  36.  *  for what exists and is selected.
  37.  *
  38.  * Parameters:
  39.  *  hDlg            HWND of the Links dialog box.
  40.  *  hList           HWND of the listbox with link items.
  41.  *
  42.  * Return Value:
  43.  *  None
  44.  */
  45.  
  46. void FAR PASCAL EnableLinkButtons(HWND hDlg, HWND hList)
  47.     {
  48.     LPOBJECT        pObj;
  49.     WORD            i;
  50.     ATOM            aCurLink=0;
  51.     WORD            cAuto=0;
  52.     WORD            cManual=0;
  53.     WORD            cStatic=0;
  54.     WORD            cUnavailable=0;
  55.     WORD            cLinks;
  56.     BOOL            fAuto;
  57.     BOOL            fManual;
  58.     BOOL            fUnavailable;
  59.     BOOL            fNone;
  60.     BOOL            fAutoOnly;
  61.     BOOL            fManualOnly;
  62.     BOOL            fUnavailableOnly;
  63.     BOOL            fUpdate;
  64.     BOOL            fChangeLink=TRUE;
  65.  
  66.  
  67.     cLinks=(WORD)SendMessage(hList, LB_GETCOUNT, 0, 0L);
  68.  
  69.     /*
  70.      * Loop through the items and count which ones are manual,
  71.      * automatic, and static.  I don't use FLinksEnumerate here because
  72.      * we have to affect so many variables.  The flow of control is not
  73.      * difficult here since we have no errors.
  74.      */
  75.     for (i=0; i < cLinks; i++)
  76.         {
  77.         //Check if this item is selected.
  78.         if (SendMessage(hList, LB_GETSEL, i, 0L))
  79.             {
  80.             //Get the data pointer for this item.
  81.             pObj=(LPOBJECT)SendMessage(hList, LB_GETITEMDATA, i, 0L);
  82.  
  83.             if (OT_STATIC==pObj->dwType)
  84.                 cStatic++;
  85.             else
  86.                 {
  87.                 switch(pObj->dwLink)
  88.                     {
  89.                     case oleupdate_always:
  90.                         cAuto++;
  91.                         break;
  92.  
  93.                     case oleupdate_oncall:
  94.                         cManual++;
  95.                         break;
  96.  
  97.                     case OLEUPDATE_UNAVAILABLE: //App-defined.
  98.                         cUnavailable++;
  99.                         break;
  100.                     }
  101.  
  102.                 /*
  103.                  * As we go through, check if each link selected is from
  104.                  * the same file.  We use pObj->aLink to compare filenames.
  105.                  *
  106.                  * If we encounter any non-matching link pathname, then
  107.                  * the Change Links button is diabled.
  108.                  */
  109.  
  110.                 if (0==aCurLink)
  111.                    aCurLink=pObj->aLink;
  112.                 else
  113.                     {
  114.                     if (aCurLink!=pObj->aLink)
  115.                         fChangeLink=FALSE;
  116.                     }
  117.                 }
  118.             }
  119.         }
  120.  
  121.     /*
  122.      * Disable all buttons if there are no manual or automatic links
  123.      * selected.  In addition, unselect both Update radiobuttons.
  124.      */
  125.  
  126.     fAuto           =(0!=cAuto);
  127.     fManual         =(0!=cManual);
  128.     fUnavailable    =(0!=cUnavailable);
  129.     fNone           =(0==cLinks || !(fAuto || fManual || fUnavailable));
  130.     fAutoOnly       =(fAuto && !fManual);
  131.     fManualOnly     =(fManual && !fAuto);
  132.     fUnavailableOnly=((0!=cUnavailable) && !fAuto && !fManual);
  133.  
  134.     fChangeLink = (fChangeLink || fUnavailableOnly) && !fNone;
  135.  
  136.     //If there are any links selected, we can update or cancel them
  137.     fUpdate=(fAuto || fManual) && !fUnavailable;
  138.     EnableWindow(GetDlgItem(hDlg, ID_UPDATENOW),  !fNone && fUpdate);
  139.     EnableWindow(GetDlgItem(hDlg, ID_CANCELLINK), !fNone && (fAuto || fManual));
  140.  
  141.     //If there are links or all links are from the same file, enable Change Link
  142.     EnableWindow(GetDlgItem(hDlg, ID_CHANGELINK), fChangeLink);
  143.  
  144.     /*
  145.      * Clear selections in the Update radiobuttons if there's no links or
  146.      * the links are not all the same type.
  147.      */
  148.  
  149.     CheckDlgButton(hDlg, ID_UPDATEAUTO,   !fNone && fAutoOnly);
  150.     CheckDlgButton(hDlg, ID_UPDATEMANUAL, !fNone && fManualOnly);
  151.  
  152.     /*
  153.      * If there are no automatic or manual links selected then disable
  154.      * the update buttons, otherwise reenable them.
  155.      */
  156.     EnableWindow(GetDlgItem(hDlg, ID_UPDATEAUTO),   !fNone);
  157.     EnableWindow(GetDlgItem(hDlg, ID_UPDATEMANUAL), !fNone);
  158.  
  159.     return;
  160.     }
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169. /*
  170.  * CchLinkStringCreate
  171.  *
  172.  * Purpose:
  173.  *  Creates a Links... listbox string from an linked object.  The
  174.  *  ObjectLink data is stored in three ATOMs in this object which we
  175.  *  created in PObjectAllocate (OLEOBJ.C).  We also append the type of link
  176.  *  (Automatic, Manual, or Unavailable) to the string.
  177.  *
  178.  *  Each string is visually limited to a tab space in the listbox.
  179.  *
  180.  * Parameters:
  181.  *  psz             LPSTR pointing to the buffer to receive the string.
  182.  *  pDoc            LPDOCUMENT containing OLE information.
  183.  *  pObj            LPOBJECT identifying object whose string we're building.
  184.  *                  We use the ATOMs from this object to create the string.
  185.  *
  186.  * Return Value:
  187.  *  WORD            Number of characters in the string.
  188.  */
  189.  
  190. WORD FAR PASCAL CchLinkStringCreate(LPSTR psz, LPDOCUMENT pDoc, LPOBJECT pObj)
  191.     {
  192.     HDC         hDC;
  193.     WORD        cch;
  194.     LPSTR       pszFile;
  195.     char        szTemp[CCHPATHMAX];
  196.  
  197.     /*
  198.      * The pszLink data is in ObjectLink format, so it immediately points
  199.      * to a classname.  We need to translate this into a real object
  200.      * name through the registration database.  This function in REGISTER.C
  201.      * is simple.
  202.      */
  203.     GetAtomName(pObj->aClass, szTemp, CCHPATHMAX);
  204.     cch=WDescriptionFromClass(szTemp, psz, CCHPATHMAX);
  205.  
  206.     if (0==cch)
  207.         return 0;
  208.  
  209.  
  210.     hDC=GetDC(pDoc->hList);
  211.  
  212.     //Limit the text and append a tab.
  213.     CchTextLimit(psz, hDC, pDoc->cxList);
  214.     lstrcat(psz, "\t");
  215.  
  216.     //Get just the filename out of the link info, limit it, and append it.
  217.     GetAtomName(pObj->aLink, szTemp, CCHPATHMAX);
  218.     pszFile=PszFileFromPath(szTemp);
  219.     CchTextLimit(pszFile, hDC, pDoc->cxList);
  220.     lstrcat(psz, pszFile);
  221.     lstrcat(psz, "\t");
  222.  
  223.  
  224.     //Append the last piece of data and a tab.
  225.     GetAtomName(pObj->aSel, szTemp, CCHPATHMAX);
  226.     CchTextLimit(szTemp, hDC, pDoc->cxList);
  227.     lstrcat(psz, szTemp);
  228.     lstrcat(psz, "\t");
  229.  
  230.  
  231.     switch (pObj->dwLink)
  232.         {
  233.         case oleupdate_always:
  234.             pszFile=PSZOLE(IDS_AUTOMATIC);
  235.             break;
  236.  
  237.         case oleupdate_oncall:
  238.             pszFile=PSZOLE(IDS_MANUAL);
  239.             break;
  240.  
  241.         case OLEUPDATE_STATIC:
  242.             pszFile=PSZOLE(IDS_STATIC);
  243.             break;
  244.  
  245.         case OLEUPDATE_UNAVAILABLE:
  246.             pszFile=PSZOLE(IDS_UNAVAILABLE);
  247.             break;
  248.         }
  249.     lstrcpy(szTemp, pszFile);
  250.     CchTextLimit(szTemp, hDC, pDoc->cxList);
  251.     lstrcat(psz, pszFile);
  252.  
  253.  
  254.     ReleaseDC(pDoc->hList, hDC);
  255.     return lstrlen(psz);
  256.     }
  257.  
  258.  
  259.  
  260.  
  261.  
  262. /*
  263.  * CchTextLimit
  264.  *
  265.  * Purpose:
  266.  *  Determines the number of character in psz that will fit in cx
  267.  *  pixels in hDC and null-terminates the string at that point.
  268.  *
  269.  * Parameters:
  270.  *  psz             LPSTR original string.
  271.  *  hDC             HDC of concern, used for GetTextExtent
  272.  *  cx              WORD number of pixels to which we limit text.
  273.  *
  274.  * Return Value:
  275.  *  WORD            Number of characters in pszDst.
  276.  */
  277.  
  278. WORD FAR PASCAL CchTextLimit(LPSTR psz, HDC hDC, WORD cx)
  279.     {
  280.     WORD            cch;
  281.     WORD            cchLow;
  282.     WORD            cchHigh;
  283.     DWORD           dwExt;
  284.  
  285.  
  286.     cch=lstrlen(psz);
  287.     cchHigh=cch;
  288.     cchLow=0;
  289.     dwExt=0L;
  290.  
  291.     while (cch!=cchLow && LOWORD(dwExt)!=cx)
  292.         {
  293.         dwExt=GetTextExtent(hDC, psz, cch);
  294.  
  295.         if (LOWORD(dwExt)==cx)
  296.             break;
  297.  
  298.         if (LOWORD(dwExt) > cx)
  299.             cchHigh=cch;
  300.  
  301.         if (LOWORD(dwExt) < cx)
  302.             cchLow=cch;
  303.  
  304.         cch=(cchHigh+cchLow) >> 1;
  305.         }
  306.  
  307.     *(psz+cch)=0;
  308.     return cch;
  309.     }
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316. /*
  317.  * LinkStringChange
  318.  *
  319.  * Purpose:
  320.  *  Changes a string in a listbox item to a new string, preserving
  321.  *  the positioning, selection, and item data of the old string.
  322.  *
  323.  * Parameters:
  324.  *  hList           HWND of the listbox.
  325.  *  i               WORD index to the item to change.
  326.  *  psz             LPSTR to the new string.
  327.  *
  328.  * Return Value:
  329.  *  None
  330.  */
  331.  
  332. void FAR PASCAL LinkStringChange(HWND hList, WORD i, LPSTR psz)
  333.     {
  334.     DWORD       dw;
  335.     DWORD       dwSel;
  336.  
  337.     dw   =SendMessage(hList, LB_GETITEMDATA,  i, 0L);
  338.     dwSel=SendMessage(hList, LB_GETSEL, i, 0L);
  339.  
  340.     SendMessage(hList, LB_DELETESTRING, i, 0L);
  341.     SendMessage(hList, LB_INSERTSTRING, i, (LONG)psz);
  342.     SendMessage(hList, LB_SETITEMDATA,  i, dw);
  343.  
  344.     if (0L!=dwSel)
  345.         SendMessage(hList, LB_SETSEL, TRUE, MAKELONG(i, 0));
  346.  
  347.     return;
  348.     }
  349.